package com.hruiworks.campusgroup.controller;

import com.hruiworks.campusgroup.common.BaseResponse;
import com.hruiworks.campusgroup.enums.SuccessCode;
import com.hruiworks.campusgroup.pojo.entity.User;
import com.hruiworks.campusgroup.service.NoticeService;
import com.hruiworks.campusgroup.utils.AuthorityUtils;
import com.hruiworks.campusgroup.utils.ResultUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/api/notice")
public class NoticeController {

    @Resource
    private NoticeService noticeService;

    /**
     * 初始化未读消息
     * @param httpServletRequest
     * @return
     */
    @GetMapping("/unread/initial")
    private BaseResponse<Integer> unreadInitial(HttpServletRequest httpServletRequest) {
        User user = AuthorityUtils.currentUser(httpServletRequest);
        Integer count = noticeService.userUnreadInitial(user);
        return ResultUtils.success(count, SuccessCode.COMMON_SUCCESS);
    }

    /**
     * 已读消息反馈
     * @param offsetStr
     * @param httpServletRequest
     * @return
     */
    @GetMapping("/read")
    private BaseResponse<Integer> readMessage(@RequestParam("offset_str") String offsetStr, HttpServletRequest httpServletRequest) {
        User user = AuthorityUtils.currentUser(httpServletRequest);
        Integer count = noticeService.userRead(user, offsetStr);
        return ResultUtils.success(count, SuccessCode.COMMON_SUCCESS);
    }
}
